﻿using System.Collections;
using System.Collections.Generic;
using BaseSyntax.Values.StringTest.Yaml.Configs;

namespace BaseSyntax.Values.StringTest.Yaml.Parsers
{
	public class ImageParser:IParser
	{
		public Dictionary<string, object> Parse(
			Dictionary<string, object> dUnity, 
			string fileId, 
			Dictionary<string, object> parent, 
			Dictionary<string, object> dCorona
			)
		{
			
			Dictionary<string, object> dSingleCorona = new Dictionary<string, object>();
			dCorona[fileId] = dSingleCorona;

			var dSingleUnity = dUnity[fileId] as Dictionary<string, object>;

			
			if (dSingleUnity == null)
			{
				return null;
			}

			var children = parent["children"] as ArrayList;
			
			// image组件作为可视组件，直接作为children
			if (children != null)
			{
				children.Add(fileId);
			}

			dSingleCorona["ctype"] = CoronaMap.GetCoronaType("Image");
			
			// 图片资源路径
			var sprite = dSingleUnity["m_Sprite"] as Dictionary<string, object>;
			
			var imgFileId = (string) sprite["fileID"];
			
			//无图片资源
			if (imgFileId == "0")
			{
				dSingleCorona["path"] = null;
				dSingleCorona["imgType"] = null;
			}
			else
			{
				if (sprite.ContainsKey("type"))
				{
					string type = (string) sprite["type"];
					//内置纯色
					if (type == "0")
					{
						dSingleCorona["path"] = null;
						dSingleCorona["imgType"] = null;
					}
					else
					{
						string guid = (string) sprite["guid"];
						dSingleCorona["path"] = ImgPathMap.GetMapper(guid);
						dSingleCorona["imgType"] = int.Parse(type);
					}
				}

				var color = dSingleUnity["m_Color"] as Dictionary<string, object>;

				if (color != null)
				{
					var dColor = new Dictionary<string, float>();
					
					dColor["r"] = float.Parse((string) color["r"]);
					dColor["g"] = float.Parse((string) color["g"]);
					dColor["b"] = float.Parse((string) color["b"]);
					dColor["a"] = float.Parse((string) color["a"]);

					dSingleCorona["color"] = dColor;
				}
			}
			
			
			
			
			
			
			return dSingleCorona;
		}
	}
}